package handler

import (
	a_adminuser "gitee.com/zoneaurora/shop/internal/logic/admin/adminuser"
	a_auth "gitee.com/zoneaurora/shop/internal/logic/admin/auth"
	a_base "gitee.com/zoneaurora/shop/internal/logic/admin/base"
	a_category "gitee.com/zoneaurora/shop/internal/logic/admin/category"

	// a_comment "gitee.com/zoneaurora/shop/internal/logic/admin/comment"
	a_order "gitee.com/zoneaurora/shop/internal/logic/admin/order"
	a_orderitem "gitee.com/zoneaurora/shop/internal/logic/admin/orderitem"
	a_product "gitee.com/zoneaurora/shop/internal/logic/admin/product"
	a_upload "gitee.com/zoneaurora/shop/internal/logic/admin/upload"
	a_user "gitee.com/zoneaurora/shop/internal/logic/admin/user"
	"gitee.com/zoneaurora/shop/internal/logic/web/auth"
	"gitee.com/zoneaurora/shop/internal/logic/web/category"
	"gitee.com/zoneaurora/shop/internal/logic/web/order"
	"gitee.com/zoneaurora/shop/internal/logic/web/orderitem"
	"gitee.com/zoneaurora/shop/internal/logic/web/product"
	"gitee.com/zoneaurora/shop/internal/middleware"
	"gitee.com/zoneaurora/shop/internal/model"

	"github.com/gin-gonic/gin"
)

func RegisterHandlers(r *gin.Engine) *gin.Engine {
	webHandle := r.Group("/")

	// category
	webHandle.GET("categories", category.GetCategories)

	//product
	webHandle.GET("products", product.GetProducts)
	webHandle.GET("products/:id", product.GetProductByID)
	webHandle.GET("cat_products", product.GetProductByCat)

	// auth
	webHandle.POST("login", auth.Login)
	webHandle.POST("register", auth.Register)

  // 以下前台aip需要jwt鉴权
  webHandle.Use(middleware.AuthUser[model.User]())

  // auth
	webHandle.GET("userinfo", auth.UserInfo)
	webHandle.PATCH("userinfo", auth.EditUserInfo)
	webHandle.PATCH("charge", auth.ChargeMoney)

	// order
	webHandle.GET("order", order.GetCurrentOrder)
	webHandle.POST("order", order.PayForOrder)
  webHandle.PATCH("order/:id", order.FinishOrder)
	webHandle.GET("orders", order.GetOrders)

	// orderitem
	webHandle.POST("orderitems", orderitem.AddOrderItem)
	webHandle.PATCH("orderitems", orderitem.ChangeNumber)
  webHandle.DELETE("orderitems/:id", orderitem.RmoveByID)
	webHandle.GET("orderitems", orderitem.GetOrderItems)

	adminHandle := r.Group("admin")

	// 初始化和登录不需要身份验证
	adminHandle.POST("auth/init", a_auth.Init)
	adminHandle.POST("auth/login", a_auth.Login)

	// 使用身份验证
	adminHandle.Use(middleware.AuthUser[model.AdminUser]())

	// 上传
	adminHandle.POST("uploads", a_upload.Upload)

	// 用户管理
	adminUserHandle := adminHandle.Group("users")
	userLogic := a_user.NewUserLogic()
	BaseHandle(adminUserHandle, userLogic)

	// 分类管理
	adminCategoryHandle := adminHandle.Group("categories")
	categoryLogic := a_category.NewCategoryLogic()
	BaseHandle(adminCategoryHandle, categoryLogic)

	// 商品管理
	adminProductHandle := adminHandle.Group("products")
	productLogic := a_product.NewProductLogic()
	BaseHandle(adminProductHandle, productLogic)

	// 管理员管理
	adminAdminUserHandle := adminHandle.Group("adminusers")
	adminUserLogic := a_adminuser.NewAdminUserLogic()
	BaseHandle(adminAdminUserHandle, adminUserLogic)

	// 订单管理
	adminOrderHandle := adminHandle.Group("orders")
	orderLogic := a_order.NewOrderLogic()
	BaseHandle(adminOrderHandle, orderLogic)

	// 订单项管理
	adminOrderItemHandle := adminHandle.Group("orderitems")
	orderItemLogic := a_orderitem.NewOrderItemLogic()
	BaseHandle(adminOrderItemHandle, orderItemLogic)

	// 评论管理 (没时间做了)
	// adminCommentHandle := adminHandle.Group("comments")
	// commentLogic := a_comment.NewCommentLogic()
	// BaseHandle(adminCommentHandle, commentLogic)

	return r
}

func BaseHandle(r *gin.RouterGroup, b a_base.IBaseLogic) {
	r.POST("", b.Create)
	r.DELETE(":id", b.Delete)
	r.PUT(":id", b.Update)
	r.GET(":id", b.Select)
	r.POST("options", b.Options)
	r.POST("page", b.GetPageList)
}
